package view.diaLogView;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.example.administrator.text.R;

import app.MyApplication;
import data.SharedPreferencesUtils;
import util.HomeUtil;
import util.LogUtil;
import util.StringUtil;
import util.Url;
import util.getInformation;

public class HomeHintPopWindow extends PopupWindow {
	private View conentView;
	private Context mContext;
	private String mIP,mNetwork,mNetFwq,mFwq,mCurrentFWQ;
    private ImageView mImageViewIP,mImageViewWay,mImageViewAgentFwq,mImageViewFwq,mImageViewNiuLang,mImageViewAiLi;// ip  网关 代理服务器 服务器 牛郎官网 阿里
    private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Bundle bundle = msg.getData();
			String type = (String) bundle.get("type");
			int state = bundle.getInt("state");
			LogUtil.eE("","handleMessage------------type"+type+" state    "+state);
			if(type.equals("ip")){
               if(state == 0){
				   mImageViewIP.setImageResource(R.mipmap.wifl_network);
			   }else {
				   mImageViewIP.setImageResource(R.mipmap.fork);
			   }
			}else if(type.equals("work")){
				if(state == 0){
					mImageViewWay.setImageResource(R.mipmap.wifl_network);
				}else {
					mImageViewWay.setImageResource(R.mipmap.fork);
				}
			}else if(type.equals("netFwq")){
				if(state == 0){
					mImageViewAgentFwq.setImageResource(R.mipmap.wifl_network);
				}else {
					mImageViewAgentFwq.setImageResource(R.mipmap.fork);
				}
			}else if(type.equals("fwq")){
				if(state == 0){
					mImageViewFwq.setImageResource(R.mipmap.wifl_network);
				}else {
					mImageViewFwq.setImageResource(R.mipmap.fork);
				}
			}else if(type.equals("taobao")){
				if(state == 0){
					mImageViewAiLi.setImageResource(R.mipmap.wifl_network);
				}else {
					mImageViewAiLi.setImageResource(R.mipmap.fork);
				}
			}else if(type.equals("altair")){
				if(state == 0){
					mImageViewNiuLang.setImageResource(R.mipmap.wifl_network);
				}else {
					mImageViewNiuLang.setImageResource(R.mipmap.fork);
				}
			}
		}
	};

	public HomeHintPopWindow(final Activity context) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.homehint_popup, null);
		this.mContext = context;
		TextView textViewName = (TextView) conentView.findViewById(R.id.text_wifinaem);
		TextView tVSiteAreaid = (TextView) conentView.findViewById(R.id.text_wifisiteAreaid);
		TextView textViewMac = (TextView) conentView.findViewById(R.id.text_wifimac);
		final TextView textViewIp = (TextView) conentView.findViewById(R.id.text_wifiip);
		final TextView textViewNetwork = (TextView) conentView.findViewById(R.id.text_wifinetwork);
		final TextView textViewAgentFwq = (TextView) conentView.findViewById(R.id.text_wifi_agentfwq);
		final TextView textViewFWQ = (TextView) conentView.findViewById(R.id.text_wififwq);
		final TextView TViewCurrentFWQ = (TextView) conentView.findViewById(R.id.text_niulang_domainname); //当前服务器
		mImageViewIP = (ImageView)conentView.findViewById(R.id.image_wifiip);
		mImageViewWay = (ImageView)conentView.findViewById(R.id.image_wifinetwork);
		mImageViewAgentFwq = (ImageView)conentView.findViewById(R.id.image_wifi_agentfwq);
		mImageViewFwq = (ImageView)conentView.findViewById(R.id.image_wifi_network);
		mImageViewNiuLang = (ImageView)conentView.findViewById(R.id.image_niulang_domainname);
		mImageViewAiLi = (ImageView)conentView.findViewById(R.id.image_taobao_domainname);

		String wifiName = getInformation.getNewGetInformation(mContext).getWIFIName();
		if(wifiName.substring(0,1).equals("\"")){
			LogUtil.eE("","进来");
			String wifi = "WiFi："+wifiName.substring(1,wifiName.length()-1);
			textViewName.setText(wifi);
		}else {
			textViewName.setText("WiFi："+wifiName);
		}

		int siteAreaid = MyApplication.getApp().getSiteAreaid();
		tVSiteAreaid.setText(""+siteAreaid);

		String ipString = getInformation.getNewGetInformation(context).getI_IP();
			mIP = ipString;
			textViewIp.setText("IP地址："+ipString);
		mNetwork = getInformation.getNewGetInformation(context).getGateWay();
		textViewMac.setText("MAC："+getInformation.getNewGetInformation(context).getIMAC());
		textViewNetwork.setText("网关："+mNetwork);
		String string  = (String) SharedPreferencesUtils.getParam(context,HomeUtil.mFWQ,"");
		if(!string.equals("") && string.contains(":8")){
			mNetFwq = string.substring(7,string.indexOf(":8"));
			textViewAgentFwq.setText("代理服务器："+mNetFwq);
		}
		if(Url.getUrlNew().getAppIP().contains(":8")){
			mFwq = Url.getUrlNew().getAppIP().substring(7,Url.getUrlNew().getAppIP().indexOf(":8"));
			textViewFWQ.setText("云服务器："+mFwq);
		}
		String fwq = MyApplication.getApp().getFWQString();
		if(fwq.contains(":8")){
			mCurrentFWQ = fwq.substring(7,fwq.indexOf(":8"));
			TViewCurrentFWQ.setText("当前服务器："+mCurrentFWQ);
		}
		setPopWindow(context);
		internet();
	}

	/**
	 * 初始化PopWindow
	 */
	public void setPopWindow(Activity context){
		int h = context.getWindowManager().getDefaultDisplay().getHeight();
		int w = context.getWindowManager().getDefaultDisplay().getWidth();
		// 设置SelectPicPopupWindow的View
		this.setContentView(conentView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(w);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 刷新状态
		this.update();
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
		// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		// 刷新状态
		this.update();
		LogUtil.eE("Window","setPopWindow");
	}

	/**
	 * 状态
	 */
	private void internet(){


			HomeUtil.mHUExecutorService.execute(new Runnable() {
				@Override
				public void run() {

						Message msg = new Message();
						Bundle b = new Bundle();// 存放数据
						int ip = StringUtil.getStringUtilNew().getPing(mIP);
						b.putString("type","ip");
						b.putInt("state",ip);
						LogUtil.eE("PING","ip"+ "state     "+ip);
						msg.setData(b);
						mHandler.sendMessage(msg);

						int work = StringUtil.getStringUtilNew().getPing(mNetwork);
						Message msg1 = new Message();
						Bundle b1 = new Bundle();
						b1.putString("type","work");
						b1.putInt("state",work);
						LogUtil.eE("","work"+ "state  "+work);
						msg1.setData(b1);
						mHandler.sendMessage(msg1);

						int netFwq = StringUtil.getStringUtilNew().getPing(mNetFwq);
						Message msg2 = new Message();
						Bundle b2 = new Bundle();
						b2.putString("type","netFwq");
						b2.putInt("state",netFwq);
						LogUtil.eE("","netFwq"+ "state  "+netFwq);
						msg2.setData(b2);
						mHandler.sendMessage(msg2);

						int fwq = StringUtil.getStringUtilNew().getPing(mFwq);
						Message msg3 = new Message();
						Bundle b3 = new Bundle();
						b3.putString("type","fwq");
						b3.putInt("state",fwq);
						LogUtil.eE("","fwq"+ "state  "+fwq);
						msg3.setData(b3);
						mHandler.sendMessage(msg3);

						int niulangGuanWan = StringUtil.getStringUtilNew().getPing(mCurrentFWQ);
						Message msg5 = new Message();
						Bundle b5 = new Bundle();
						b5.putString("type","altair");
						b5.putInt("state",niulangGuanWan);
						LogUtil.eE("","altair"+ "state  "+niulangGuanWan);
						msg5.setData(b5);
						mHandler.sendMessage(msg5);


					   int taobao = StringUtil.getStringUtilNew().getPing("www.taobao.com");
					   Message msg4 = new Message();
					   Bundle b4 = new Bundle();
					   b4.putString("type","taobao");
					   b4.putInt("state",taobao);
					   LogUtil.eE("PING","taobao"+ "state  "+taobao);
					   msg4.setData(b4);
					   mHandler.sendMessage(msg4);
				}
			});
	}
}
